const express = require('express')
const jsonServer = require('json-server')
const Axios = require('axios')
const bcryptjs = require('bcryptjs')
const routes = jsonServer.router('./db.json')
const middlewares = jsonServer.defaults()
Axios.defaults.baseURL = 'http://localhost:9090'
const server = express()


//req.body处理
server.use(express.json())
server.use(express.urlencoded({ extended: true }))

//跨域处理
server.use((req, res, next) => {
  res.set('Access-Control-Allow-Origin', '*')
  next()
})

//延迟处理
const timer = (time = 100) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve()
    }, time)
  })
}
server.use(async (req, res, next) => {
  await timer()
  next()
})
server.post('/sign-up', async (req, res) => {
  const response = await Axios.get('/users', { params: { username: req.body.username } })
  if (response.data.length > 0) {
    res.send({
      code: -1,
      msg: '用户名已被注册'
    })
    return
  }
  const { data } = await Axios.post('/users', {
    ...req.body,
    password: await bcryptjs.hash(req.body.password, 10)
  })
  res.send({
    code: 0,
    msg: '注册成功',
    data
  })
})

server.post('/sign-in', async (req, res) => {
  const { username, password } = req.body
  const { data } = await Axios.get('/users', {
    params: {
      username
    }
  })
  if (data.length <= 0) {
    res.send({
      code: -1,
      msg: '用户名或密码错误'
    })
    return
  }
  const user = data[0]
  const isOk = await bcryptjs.compare(password, user.password)
  if (isOk) {
    res.send({
      code: 0,
      msg: '登录成功',
      data: data
    })
  } else {
    res.send({
      code: -1,
      msg: '用户名或密码错误'
    })
  }
})

server.post('/peoplemsg', async (req, res) => {
  let resList = await Axios.get('/findPeopleData')
  let length = resList.data.length
  const { peoplemsg, page, limit } = req.body
  if (peoplemsg !== 'abcdefg') {
    return
  }
  const { data } = await Axios.get('/findPeopleData', {
    params: {
      _page: page,
      _limit: limit
    }
  })
  if (data.length > 0) {
    res.send({
      code: 0,
      msg: '请求数据成功',
      data,
      length
    })
  } else {
    res.send({
      code: -1,
      msg: '数据请求失败'
    })
  }
})

server.post('/filtrateData', async (req, res) => {
  if (!req.body) {
    return
  }
  const { data } = await Axios.get('/findPeopleData', {
    params: req.body
  })
  if (data.length > 0) {
    res.send({
      code: 0,
      msg: '请求数据成功',
      data
    })
  } else {
    res.send({
      code: -1,
      msg: '数据请求失败'
    })
  }
})

server.post('/settabledata', async (req, res) => {
  const { papersNum } = req.body
  const { data } = await Axios.get('/setPeople', {
    params: { "papersNum": papersNum }
  })
  console.log(data)
  if (data.length > 0) {
    res.send({
      code: -1,
      msg: '请勿添加重复数据',
    })
  } else {
    await Axios.post('/setPeople', {
      ...req.body
    })
    const response = await Axios.get('/setPeople')
    res.send({
      code: 0,
      msg: '数据添加成功',
      data: response.data
    })
  }
})

server.post('/deltablelist', async (req, res) => {
  const { id } = req.body
  if (id) {
    await Axios.delete(`/setPeople/${id}`)
    const { data } = await Axios.get('/setPeople')
    res.send({
      code: 0,
      msg: '删除成功',
      data
    })
  } else {
    res.send({
      code: -1,
      msg: '删除失败'
    })
  }
})

server.use(middlewares)
server.use(routes)
server.listen(9090)
